package com.team2.outpatient_sys.service.impl;

import com.github.pagehelper.PageHelper;
import com.team2.outpatient_sys.dao.*;
import com.team2.outpatient_sys.pojo.*;
import com.team2.outpatient_sys.service.RegisteredService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class RegisteredServiceImpl implements RegisteredService {
    @Autowired
    private RegisteredDao registeredDao;
    @Autowired
    private PatientDao patientDao;
    @Autowired
    private OutpatientScheduleDao outpatientScheduleDao;

    @Override
    public boolean add(Registered registered) {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String regTime = sdf.format(date);
        registered.setRegTime(regTime);
        registered.setStatus(1);
        return registeredDao.add(registered) < 1 ? false : true;
    }

    @Override
    public boolean update(Registered registered) {
        return registeredDao.update(registered) < 1 ? false : true;
    }

    @Override
    public List<Registered> findAll() {
        return registeredDao.findAll();
    }

    @Override
    public Registered findById(int id) {
        return registeredDao.findById(id);
    }

    @Override
    public boolean delete(int id) {
        return registeredDao.delete(id) < 1 ? false : true;
    }

    @Override
    public List<Registered> findByPatientId(int patientId) {
        return registeredDao.findByPatientId(patientId);
    }

    @Override
    public List<Registered> findByDoctorId(int doctorId) {
        return registeredDao.findByDoctorId(doctorId);
    }

    @Override
    public List<Registered> findByDeptId(int deptId) {
        return registeredDao.findByDeptId(deptId);
    }

    @Override
    public List<Registered> findByClinicTime(String clinicTime) {
        return registeredDao.findByClinicTime(clinicTime);
    }

    @Override
    public boolean offlineAdd(RegisteredOffline registeredOffline) {
        String idCard = registeredOffline.getIdCard();
        Patient patient = patientDao.findByIdCard(idCard);
        int patientId;
        if (patient == null) {
            Patient patient1 = new Patient();
            patient1.setName(registeredOffline.getName());
            patient1.setIdCard(idCard);
            String birth = idCard.substring(6, 10) + "-" + idCard.substring(10, 12) + "-" + idCard.substring(12, 14);
            patient1.setBirth(birth);
            int gender = Integer.parseInt(String.valueOf(idCard.charAt(16)));
            if (gender % 2 == 0) {
                patient1.setGender("女");
            } else {
                patient1.setGender("男");
            }
            patientDao.add(patient1);
            patientId = patientDao.findByIdCard(idCard).getId();
        } else {
            patientId = patient.getId();
        }
        Registered registered = new Registered();
        registered.setPatientId(patientId);
        registered.setClinicTime(registeredOffline.getClinicTime());
        registered.setDeptId(registeredOffline.getDeptId());
        registered.setDoctorId(registeredOffline.getDoctorId());
        registered.setRegTime(registeredOffline.getRegTime());
        registered.setTimeQuantum(registeredOffline.getTimeQuantum());
        registered.setStatus(registeredOffline.getStatus());
        registered.setUserId(0);
        registered.setSerialNumber(15);
        return registeredDao.add(registered) < 1 ? false : true;
    }

    @Override
    public Registered findByUserId(Long userId) {
        return registeredDao.findByUserId(userId);
    }

    @Override
    public void updateByPatentTime(Registered registered) {
         registeredDao.updateByPatentTime(registered);
    }

    @Override
    public List<RegisteredDetailed> findRegisteredDetailed(Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize,true);
        List<RegisteredDetailed> list = registeredDao.findRegisteredDetailed();
        return list;
    }

    @Override
    public boolean overdue(String clinicTime) {
        return  registeredDao.overdue(clinicTime) < 1 ? false : true;
    }

    @Override
    public List<DoctorAndAppointment> findDoctorAndAppointment(Integer deptId, Integer week) {
        return outpatientScheduleDao.findDoctorAndAppointment(deptId,week);
    }

    @Override
    public List<Doctor> findDoctor(Integer week, Integer deptId) {
        return registeredDao.findDoctor(week, deptId);
    }

    @Override
    public List<DoctorShift> findDoctorShift(Integer week, Integer deptId, Integer doctorId) {
        return registeredDao.findDoctorShift(week, deptId, doctorId);
    }

    @Override
    public boolean registeredSuccess(Integer doctorId, Integer scheduleId) {
        return registeredDao.registeredSuccess(doctorId, scheduleId) < 1 ? false : true;
    }

    @Override
    public List<Registered> findSign(String clinicTime,Integer currentPage, Integer pageSize) {
        PageHelper.startPage(currentPage,pageSize,true);
        List<Registered> list = registeredDao.findSign(clinicTime);
        return list;
    }
}
